package com.skpro;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @author smark
 * 代码自动生成配置类
 */
public class SkAutoGenerator {

    public static void main(String[] args) {

        //1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        //2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("smark");
        gc.setOpen(false);
        gc.setFileOverride(false);//重新生成文件的时候是否会覆盖
        gc.setIdType(IdType.ID_WORKER_STR);//主键策略
        //实体属性 Swagger2 注解
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);


        //3、数据源配置

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/douyingdb?serverTimezone=GMT%2B8&characterEncoding=utf-8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        //4、生成的包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("mbtis");
        pc.setParent("com.skpro");
        pc.setEntity("entity");
        pc.setController("controller");
        pc.setMapper("mapper");
        pc.setService("service");
        mpg.setPackageInfo(pc);

        //5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据映射到实体类的策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);//自动添加lombok的注解
        strategy.setRestControllerStyle(true);//restFul
        strategy.setControllerMappingHyphenStyle(true);//url 驼峰命名 转化为_

        //逻辑删除
//        strategy.setLogicDeleteFieldName("is_deleted");
        //strategy.setEntityBooleanColumnRemoveIsPrefix(true);//去除布尔值的is_前缀

        //自动填充
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFill = new ArrayList();
        tableFill.add(createTime);
//        tableFill.add(updateTime);
        strategy.setTableFillList(tableFill);

        //乐观锁的列
//        strategy.setVersionFieldName("version");

        mpg.setStrategy(strategy);

        //执行
        mpg.execute();


    }
}
